﻿using System;
using System.Web.Mvc;
using Pulsus;
using SlamCms.Data.Events;
using SlamCms.DomainEvents;
using SlamCms.Web;
using SlamCms.Web.Mvc;

namespace SlamCms.Areas._admin.Controllers
{
	[AuthorizeTo(AuthorizeTo.All)]
	public class CacheController : AdminBaseController
	{
		private readonly SlamCmsContext _slamCmsContext;

		public CacheController(SlamCmsContext slamCmsContext)
		{
			_slamCmsContext = slamCmsContext;
		}

		[HttpGet]
		[OutputCache(Duration = 0)]
		public ActionResult Invalidate(string type, string id)
		{
			switch (type.ToLowerInvariant())
			{
				case "tags":
					_slamCmsContext.Data.Cache.InvalidateTags();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Tags));
					break;
				case "users":
					_slamCmsContext.Data.Cache.InvalidateUsers();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Users));
					break;
				case "core":
					_slamCmsContext.Data.Cache.InvalidateCore();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Core));
					break;
				case "sitemaps":
					_slamCmsContext.Data.Cache.InvalidateSitemaps();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Sitemaps));
					break;
				case "queries":
					_slamCmsContext.Data.Cache.InvalidateQueries();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Queries));
					break;
				case "content":
					// assume it's a ContentItemId -> invalidate all for now
					_slamCmsContext.Data.Cache.InvalidateQueries();
					DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Content));
					break;
			}

			if (Request.AcceptTypes != null && (Request.AcceptTypes[0] ?? string.Empty).Equals("application/json", StringComparison.InvariantCultureIgnoreCase))
				return Json(new { Ok = true }, JsonRequestBehavior.AllowGet);

			return this.RedirectToReferer();
		}

		[HttpGet]
		[OutputCache(Duration = 0)]
		public ActionResult InvalidateContentItem(string id)
		{
			// temporary, for now we invalidate all slam queries
			_slamCmsContext.Data.Cache.InvalidateQueries();

			DomainEventsManager.Raise(new CacheInvalidated(CacheInvalidatedType.Content));
			DomainEventsManager.Raise(new ContentInvalidated(id));

			if (Request.AcceptTypes != null && (Request.AcceptTypes[0] ?? string.Empty).Equals("application/json", StringComparison.InvariantCultureIgnoreCase))
				return Json(new { Ok = true }, JsonRequestBehavior.AllowGet);

			return this.RedirectToReferer();
		}

		[HttpGet]
		[OutputCache(Duration = 0)]
		public ActionResult InvalidateAllQueries()
		{
			_slamCmsContext.Data.Cache.InvalidateQueries();

			if (Request.AcceptTypes != null && (Request.AcceptTypes[0] ?? string.Empty).Equals("application/json", StringComparison.InvariantCultureIgnoreCase))
				return Json(new { Ok = true }, JsonRequestBehavior.AllowGet);

			return this.RedirectToReferer();
		}
	}
}